package testMicroservice

import (
	"autoTestNew/pkg/commonType"
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	ctrl "sigs.k8s.io/controller-runtime"
)

var log = ctrl.Log.WithName("mongodb/testMicroservice")

type Store struct {
	collection *mongo.Collection
}

func NewStore(db *mongo.Database) *Store {
	return &Store{
		collection: db.Collection("testMicroservice"),
	}
}

// Create 创建测试微服务
func (s *Store) Create(ctx context.Context, microservice commonType.TestMicroservice) (commonType.TestMicroservice, error) {
	if microservice.ID == nil {
		newID := primitive.NewObjectID()
		microservice.ID = &newID
	}

	result, err := s.collection.InsertOne(ctx, microservice)
	if err != nil {
		log.Error(err, "insert microservice error", "microservice", microservice)
		return commonType.TestMicroservice{}, err
	}

	objectID, ok := result.InsertedID.(primitive.ObjectID)
	if !ok {
		log.Error(fmt.Errorf("type assertion failed"), "Failed to assert type for InsertedID")
		return commonType.TestMicroservice{}, fmt.Errorf("type assertion failed for InsertedID")
	}

	microservice.ID = &objectID
	return microservice, nil
}

// Get 获取测试微服务
func (s *Store) Get(ctx context.Context, id primitive.ObjectID) (commonType.TestMicroservice, error) {
	var testMicroservice commonType.TestMicroservice
	err := s.collection.FindOne(ctx, primitive.M{"_id": id}).Decode(&testMicroservice)
	if err != nil {
		log.Error(err, "get testMicroservice error", "id", id)
		return commonType.TestMicroservice{}, err
	}
	return testMicroservice, nil
}

// 根据name 与 recordId获取测试微服务
func (s *Store) GetByNameAndRecordId(ctx context.Context, name string, recordId primitive.ObjectID) (commonType.TestMicroservice, error) {
	var testMicroservice commonType.TestMicroservice
	err := s.collection.FindOne(ctx, primitive.M{"name": name, "record_id": recordId}).Decode(&testMicroservice)
	if err != nil {
		log.Error(err, "get testMicroservice error", "name", name, "recordId", recordId)
		return commonType.TestMicroservice{}, err
	}
	return testMicroservice, nil
}

// List 获取测试微服务列表
func (s *Store) List(ctx context.Context) ([]commonType.TestMicroservice, error) {
	var testMicroservice []commonType.TestMicroservice
	cursor, err := s.collection.Find(ctx, primitive.M{})
	if err != nil {
		log.Error(err, "list testMicroservice error, find error")
		return nil, err
	}
	err = cursor.All(ctx, &testMicroservice)
	if err != nil {
		log.Error(err, "list testMicroservice error, decode error")
		return nil, err
	}
	return testMicroservice, nil
}

// Update 更新测试微服务
func (s *Store) Update(ctx context.Context, id primitive.ObjectID, testMicroservice commonType.TestMicroservice) error {
	_, err := s.collection.UpdateOne(ctx, primitive.M{"_id": id}, primitive.M{"$set": testMicroservice})
	if err != nil {
		log.Error(err, "update testMicroservice error", "id", id, "testMicroservice", testMicroservice)
		return err
	}
	return nil
}

// Delete 删除测试微服务
func (s *Store) Delete(ctx context.Context, id primitive.ObjectID) error {
	_, err := s.collection.DeleteOne(ctx, primitive.M{"_id": id})
	if err != nil {
		log.Error(err, "delete testMicroservice error", "id", id)
		return err
	}
	return nil
}

// DeleteAll 删除所有测试微服务
func (s *Store) DeleteAll(ctx context.Context) error {
	_, err := s.collection.DeleteMany(ctx, primitive.M{})
	if err != nil {
		log.Error(err, "delete all testMicroservice error")
		return err
	}
	return nil
}
